package com.itheima.test;

import java.util.Random;

public class test9 {
    public static void main(String[] args) {
        //直播抽奖
        //奖品是现金红包{2,588,888,1000,10000}

        //定义数组记录奖品
        int[] arr = {2,588,888,1000,10000};
        //定义新数组存储抽奖结果
        int[] newArr = new int[arr.length];
        //抽奖
        //有5个奖项
        Random r = new Random();
        for (int i = 0; i < 5; ) {
            //获取随机索引
            int randomIndex = r.nextInt(arr.length);
            //抽取奖项
            int prize = arr[randomIndex];
            //判断是否存在，存在则重新抽取，不存在则表示有效奖项
            if(!contains(newArr, prize)) {
                newArr[i] = prize;
                i++;
            }//存在则重新抽取，i不变
        }

        //遍历数组
        for (int i = 0; i < newArr.length; i++) {
            System.out.println(newArr[i]);
        }
    }

    //判断prize在arr数组中是否存在
    public static boolean contains(int[] arr,int prize){
        for (int i = 0; i < arr.length; i++) {
            if(arr[i]==prize){
                return true;
            }
        }
        return false;
    }
}
